package com.guet.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guet.common.utils.R;
import com.guet.main.entity.Room;
import com.guet.main.entity.vo.RoomVo;
import com.guet.main.service.RoomService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * room 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-07-28
 */
@RestController
@RequestMapping("/main/room")
@CrossOrigin
public class RoomController {
    @Autowired
    private RoomService roomService;

    @GetMapping("/list")
    @ApiOperation("获取所有房间")
    public R list(){
        List<Room> list = roomService.list(null);
        return R.ok().data("list",list);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("通过房间ID获取数据")
    public R getById(@PathVariable String id){
        Room byId = roomService.getById(id);
        return R.ok().data("room",byId);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("通过删除ID")
    public R delete(@PathVariable String id){
        boolean b = roomService.removeById(id);
        return b?R.ok():R.error();
    }

    @PutMapping("/update")
    @ApiOperation("更新")
    public R update(@RequestBody Room room){
        boolean b = roomService.updateById(room);
        return b?R.ok():R.error();
    }

    @PostMapping("/add")
    @ApiOperation("增加会议室信息")
    public R add(@RequestBody RoomVo roomVo){
        Room room = new Room();
        BeanUtils.copyProperties(roomVo,room);
        boolean save = roomService.save(room);
        return save?R.ok(): R.error();
    }

    @GetMapping("/getByQuery/{kind}/{capacity}")
    @ApiOperation("条件查询会议室")
    public R getByQuery(@PathVariable String kind,@PathVariable String capacity){
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        if(!kind.equals("null")){
            queryWrapper.eq("kind",kind);
        }
        if(!capacity.equals("null")){
            queryWrapper.ge("capacity",capacity);
        }
        List<Room> list = roomService.list(queryWrapper);
        return R.ok().data("list",list);
    }
}

